package com.hdd.utils.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.google.gson.Gson;
import com.hdd.config.Constant;
import com.hdd.dto.SuperResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AliPayUtils {




    /**
     * 支付宝向用户转账
     * @param bizNo  逻辑单号
     * @param amount 转账金额 "1.21"单位元
     * @param account 支付宝账号
     * @param userName 支付宝真实姓名
     * @return
     */
    public static SuperResponse alipay2User(String bizNo, String amount, String account, String userName){
        AlipayClient alipayClient=new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                Constant.ZFB_APP_KEY, Constant.ZFB_PRIVATE_KEY, "json","UTF-8",Constant.ZFB_PUBLIC_KEY,"RSA2");
        SuperResponse res = new SuperResponse();
        AlipayVo vo = new AlipayVo();
        vo.setOut_biz_no(bizNo);
        vo.setPayee_type("ALIPAY_LOGONID");
        vo.setAmount(amount);
        vo.setPayee_account(account);
        vo.setPayer_show_name("画多多");
        vo.setPayee_real_name(userName);
        vo.setOrder_title("画多多");
        vo.setRemark("画多多提现");
        String json = new Gson().toJson(vo);
        // 设置请求参数
        AlipayFundTransToaccountTransferRequest alipayRequest = new AlipayFundTransToaccountTransferRequest();
        alipayRequest.setBizContent(json);
        AlipayFundTransToaccountTransferResponse response=null;
        try {
            response = alipayClient.execute(alipayRequest);
            if("10000".equals(response.getCode())){
                res.setItem(response.getOrderId());
                res.setMessage(response.getOutBizNo());
            }else{
                res.setCode(-1000);
                res.setMessage("转账失败:"+response.getSubMsg());
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            res.setCode(-1000);
            res.setMessage("转账异常:"+e.getErrMsg());
        }
        log.error(res.toString());
        return res;
    }


    public static SuperResponse query(){
        SuperResponse res = new SuperResponse();
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                Constant.ZFB_APP_KEY,Constant.ZFB_PRIVATE_KEY,"json","GBK",Constant.ZFB_PUBLIC_KEY,"RSA2");
        AlipayFundAccountQueryRequest request = new AlipayFundAccountQueryRequest();
        request.setBizContent("{" +
                "\"alipay_user_id\":\"2088931781809006\"," +
                "\"account_type\":\"ACCTRANS_ACCOUNT\"," +
                "\"account_scene_code\":\"SCENE_000_000_000\"" +
                " }");
        try {
            AlipayFundAccountQueryResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                res.setItem(response.getAvailableAmount());
            } else {
                res.setCode(-1000);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        log.error(res.toString());
        return res;
    }


    public static  void order_Query() throws Exception{
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                Constant.ZFB_APP_KEY,Constant.ZFB_PRIVATE_KEY,"json","UTF-8",Constant.ZFB_PUBLIC_KEY,"RSA2");
        AlipayFundTransCommonQueryRequest request = new AlipayFundTransCommonQueryRequest();
        request.setBizContent("{" +
                "\"product_code\":\"STD_RED_PACKET\"," +
                "\"biz_scene\":\"PERSONAL_PAY\"," +
                "\"out_biz_no\":\"10000000009\"," +
                "\"order_id\":\"20201210110070001506290077586166\"," +
                "\"pay_fund_order_id\":\"20190801110070001506380000251556\"" +
                "  }");
        AlipayFundTransCommonQueryResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }


    public static void main(String[] args) throws Exception {
       AliPayUtils.alipay2User("10000000006","0.1","fzh6185129@126.com","方泽华");
      //  AliPayUtils.query();
    }
}